package com.dev;

import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

import com.dev.user.model.User;

@ManagedBean(name="userBean")
@SessionScoped

public class UserBean {

	public List<User> getUserList(){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<User> list = new ArrayList<User>();
		
		try{
			
			System.out.println(" inside try block >>>>>> ");

			Class.forName("com.mysql.jdbc.Driver");
			con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/lportal", "root","root");
			String query="Select * from lms_lmsbook";
			ps = (PreparedStatement) con.prepareStatement(query);
			rs = (ResultSet) ps.executeQuery();
			System.out.println(" rs >>>>>> "+rs);
			while(rs.next()){
				User user = new User();
				user.setBookId(rs.getLong("bookId"));
				user.setBookTitle(rs.getString("bookTitle"));
				user.setAuthor(rs.getString("author"));
				user.setDateAdded(rs.getDate("dateAdded"));
				list.add(user);
			}

		}catch(Exception e){
			e.printStackTrace();

		}finally{
			try{
				con.close();
				ps.close();

			}catch(Exception e){
				e.printStackTrace();
			}
		}

		return list;

	}

}
